package com.ahoi.common.utils;


import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * author: ahoi
 * date: 2018/8/30 17:50
 * description:
 */
public class HttpUtils {

    private static Logger logger = LoggerFactory.getLogger(HttpUtils.class);

    /**
     * POST请求
     */
    public static String postJson(String url, String jsonParams) {
        String result = "";
        // 构造http
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost request = new HttpPost(url);
        if (StringUtils.isNotEmpty(jsonParams)) {
            request.setEntity(new StringEntity(jsonParams, "UTF-8"));
            request.setHeader("content-type", "application/json");
        }

        // 设置网络超时
        RequestConfig config = RequestConfig.custom()
                .setConnectionRequestTimeout(3 * 1000)
                .setConnectTimeout(3 * 1000)
                .setSocketTimeout(3 * 1000)
                .build();
        request.setConfig(config);

        // 发送请求
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(request);
            // 获取返回数据
            HttpEntity entry = response.getEntity();
            int code = response.getStatusLine().getStatusCode();
            if (code == 200) {
                result = EntityUtils.toString(entry);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            request.releaseConnection();
            HttpClientUtils.closeQuietly(response);
            HttpClientUtils.closeQuietly(httpClient);
        }
        return result;
    }

    public static String get(String url) {
        String content = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        try {
            CloseableHttpResponse response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            content = EntityUtils.toString(entity, "UTF-8");
        } catch (ClientProtocolException e) {
            logger.info("GET客户端连接异常：" + e.getMessage());
        } catch (IOException e) {
            logger.info("GET读取认证中心数据IO异常：" + e.getMessage());
        } finally {
            httpGet.releaseConnection();
            try {
                httpClient.close();
            } catch (IOException e) {
                logger.info("GET连接关闭异常：" + e.getMessage());
            }
        }
        return content;
    }
}
